Optimize calculation of 2-sample mean in MF_steps_ahead#83
Conversation
Previous implementation uses a 2nd order linear recurrence relation to compute the 2-sample mean predictor at a given time index. It is replaced by the closed-form solution that is faster to compute.
|
Ok, thanks for the very clear PR! I assume you've validated this change in yielding equivalent performance? I don't see any evidence of this. |
|
I am not entirely sure what you mean about yielding equivalent performance. Perhaps I can add the results of some tests I did with the Bonn EGG dataset. There are 4 master operations that are based on The first run of the This is counterbalanced by all the other calls that are slightly faster on average. If I compare the timing for each time series this is what I get (
NB: One call to For the whole dataset (N=500, 4 master operations), the speedup amounts to 74 s on a total computation time of ~6 min (18-19 s per master operation). I noticed that subsequent runs have a smaller speedup (0.025 s per time series, 10-12 s per master operation, 44 s for the whole dataset) because Parallel computation is one scenario I have not tested. I agree that the I hope this helps. Let me know if there is anything else I can do. |
This PR optimises the calculation of the 2-sample mean in the
MF_steps_aheadoperation.Mathematically reframed, the code above iteratively calculates the terms of the following sequence (p subscript is for predict):
Let$y_j$ and $y_{j+1}$ be 2 consecutive samples of $y$ .
which gives the value of the 2-sample mean predictor at$j+n$ . This is a linear recurrence relation of order 2. It admits a closed-form expression that you can work out (see e.g. Wikipedia article).
Note that I did a change of variable so that$i$ is the prediction length. Under this form, the "weights" used in the 2-sample mean can be computed directly regardless of the prediction length. This also implies that it is no longer needed to iterate "manually" over the samples of the time series using a for loop. The code above can then be rewritten as a simple matrix product:
This piece of code runs between tens to thousands of times faster on my laptop, depending on the length of the time series and the prediction length.
